package com.goodboys.service.impl;

import com.goodboys.base.entity.BaseReqBean;
import com.goodboys.base.resp.UserTagRespBean;
import com.goodboys.base.service.impl.BaseServiceImpl;
import com.goodboys.dao.InfoTagDao;
import com.goodboys.dao.UserTagDao;
import com.goodboys.entity.InfoTag;
import com.goodboys.entity.UserTag;
import com.goodboys.service.TagService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by CJ on 2018-05-25.
 */
@Service("tagService")
public class TagServiceImpl implements TagService{

    private static Logger logger = LoggerFactory.getLogger(TagServiceImpl.class);

    @Autowired
    protected UserTagDao userTagDao;

    @Autowired
    protected InfoTagDao infoTagDao;


    @Override
    public Map addUserTag(BaseReqBean baseReqBean) throws Exception {
        Map map = new HashMap(); map.put("status",0);
        if (baseReqBean != null && !StringUtils.isEmpty(baseReqBean.getId()) &&
                !StringUtils.isEmpty(baseReqBean.getName()) && baseReqBean.getTag() !=null
                && baseReqBean.getType() != null){
            UserTag userTag = convertUserTag(baseReqBean);
            userTagDao.insertSelective(userTag);
            //返回该用户所有的自定义id
//            userTagDao.selectByPrimaryKey(userTag);
            //返回该用户下所有的tags
            //这里设置最大返回20个标签
            baseReqBean.setSize(20);
            baseReqBean.setFlag(0);//0:用户标签，1：信息标签
            return  findTags(baseReqBean);//直接返回用户的所有标签
        }else {
            map.put("message", "参数值存在为空，请重试。。。");
            logger.error("ERROR=====>addUserTag:"+baseReqBean.getId()+"=====参数存在为空");
            return map;
        }
    }


    /**
     * 判断此标签是否已经重复
     * @param baseReqBean
     * @return
     * @throws Exception
     */
    @Override
    public Map isExistUserTag(BaseReqBean baseReqBean) throws Exception {
        Map map = new HashMap(); map.put("status",0);
        if (baseReqBean != null && !StringUtils.isEmpty(baseReqBean.getId()) &&
                !StringUtils.isEmpty(baseReqBean.getName()) && baseReqBean.getType() !=null){
            UserTag userTag = convertUserTag(baseReqBean);
            Integer count = userTagDao.isExistUserTag(userTag);
            if(count > 0){
                map.put("message", "自定义标签已存在。。。");
            }else {
                map.put("status", 1);
                map.put("message", "该标签标签可创建。");
            }
        }else {
            map.put("message", "参数值存在为空，请重试。。。");
            logger.error("ERROR=====>isExistUserTag:"+baseReqBean.getId()+"=====参数存在为空");
        }
        return map;
    }

    @Override
    public Map deleteUserTag(UserTag userTag) throws Exception {
        Map map = new HashMap(); map.put("status",0);
        if (userTag != null && userTag.getId() != null){
            //处理之前先判断是否删除为本人删除


            //判断是否一级标签(一级标签不支持删除)
            UserTag selectUserTag = userTagDao.selectByPrimaryKey(userTag);
            if(selectUserTag != null && selectUserTag.getParentId() == -1){
                map.put("message", "一级标签不支持删除。。。");
            }else {
                //开始删除
                userTagDao.deleteByUserTag(userTag);
                //查询用户下的所有标签
                map.put("status", 1);
                map.put("message", selectUserTag.getName() +"删除成功。");
            }
        }else {
            map.put("message", "参数值存在为空，请重试。。。");
            logger.error("ERROR=====>deleteUserTag:"+userTag.getId()+"=====参数存在为空");
        }
        return map;
    }

    @Override
    public Map updateUserTag(UserTag userTag) throws Exception {
        Map map = new HashMap(); map.put("status",0);
        if (userTag != null && userTag.getId() != null && !StringUtils.isEmpty(userTag.getName())
                && !StringUtils.isEmpty(userTag.getUserId())){
            //处理之前先判断是否为本人更新

            //判断是否一级标签(一级标签不支持更新)
            UserTag selectUserTag = userTagDao.selectByPrimaryKey(userTag);
            if(selectUserTag != null && selectUserTag.getParentId() == -1){
                map.put("message", "一级标签不支持更新。。。");
            }else {
                //开始更新
                userTag.setUpdateTime(new Date());
                userTagDao.updateByPrimaryKeySelective(userTag);
                //查询用户下的所有标签
                //这里设置最大返回20个标签
                BaseReqBean baseReqBean = new BaseReqBean();
                baseReqBean.setId(userTag.getUserId());
                baseReqBean.setSize(20);
                baseReqBean.setFlag(0);//0:用户标签，1：信息标签
                map.put("status", 1);
                map.put("message", findTags(baseReqBean));
            }

        }else {
            map.put("message", "参数值存在为空，请重试。。。");
            logger.error("ERROR=====>deleteUserTag:"+userTag.getId()+"=====参数存在为空");
        }
        return map;
    }


    /**
     * 查询用户/信息标签
     * @param baseReqBean
     * @return
     * @throws Exception
     */
    @Override
    public Map findTags(BaseReqBean baseReqBean) throws Exception {
        Map map = new HashMap(); map.put("status",0);
        map.put("page", baseReqBean.getPage()-1);
        map.put("size", baseReqBean.getSize());
        map.put("type", baseReqBean.getType());
        map.put("userId", baseReqBean.getId());

        //查询信息的标签
        if (baseReqBean.getFlag() != null && baseReqBean.getFlag() == 1) {
            map.put("list", infoTagDao.findTagsByType(map));
            map.put("total", infoTagDao.findTagsByTypeCount(map));
            map.put("attribute", 1);
        }else {
            //查询用户的标签
            //这里两种处理逻辑
            if(!StringUtils.isEmpty(baseReqBean.getId())) {
                List<UserTagRespBean> tagsById = userTagDao.findTagsById(map);
                map.put("message", (tagsById.isEmpty())?null:userTagDao.findTagsById(map).get(0));
                map.put("total", userTagDao.findTagsByIdCount(map));
                map.put("attribute", 0);

            }else {
                map.put("list", userTagDao.findTagsByType(map));
                map.put("total", userTagDao.findTagsByTypeCount(map));
                map.put("attribute", 1);
            }
        }
        map.put("status", 1);
        return map;
    }





    


    public static UserTag convertUserTag(BaseReqBean baseReqBean){
        UserTag userTag = new UserTag();
        userTag.setUserId(baseReqBean.getId());
        userTag.setName(baseReqBean.getName());
        userTag.setCreateTime(new Date());
        userTag.setParentId(baseReqBean.getTag());
        userTag.setType(baseReqBean.getType());
        return userTag;
    }


}
